package my_web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Proxy.DynamicProxy;
import ReqBean.RequestBean;
import my_web.Interface.LeaveInterface;
import my_web.bean.Emp;
import my_web.bean.Leave;
import my_web.model.LeaveDto;
import my_web.service.LeaveService;
public class LeaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		//res.setCharacterEncoding("utf-8");
		res.setContentType("text/html;charset=UTF-8 ");
		//PrintWriter out=res.getWriter();
		String path=req.getServletPath();
		HttpSession session=req.getSession();
		LeaveInterface service = null;
		try {
			service=DynamicProxy.creatServiceProxy(LeaveInterface.class,LeaveService.class);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if("/LeaveToList.do".equals(path)){
			try {
				Emp emp=(Emp)session.getAttribute("Emp");
				LeaveDto dto=RequestBean.getBeanFromRequest(LeaveDto.class, req);
				req.setAttribute("pg",service.DynamicQueryAllLeaves(dto,emp));
				req.getRequestDispatcher("leaveList.jsp").forward(req, res);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("/LeaveToAdd.do".equals(path)){
			req.getRequestDispatcher("leaveAdd.jsp").forward(req, res);
		}else if("/LeaveDoAdd.do".equals(path)){
			LeaveDto dto = null;
			try {
				dto=RequestBean.getBeanFromRequest(LeaveDto.class, req);
				dto.setE_id(((Emp) session.getAttribute("Emp")).getE_id());
				dto.setQj_id(System.currentTimeMillis()+dto.getE_id());
				req.setAttribute("leave",dto);
				service.addLeave(dto);	
				res.sendRedirect("LeaveToList.do");
			} catch (Exception e) {
				e.printStackTrace();
			}	
		}else if("/LeaveDoDel.do".equals(path)){
			res.setContentType("application/json;charset=utf-8");
			PrintWriter out=res.getWriter();
			try {
				String qj_id=req.getParameter("qj_id");
				service.removeLeaveById(qj_id);
				out.print("{\"success\":true,\"msg\":\"删除成功！\"}");
			} catch (Exception e) {
				e.printStackTrace();
				out.print("{\"success\":false,\"msg\":\"删除失败！\"}");
			}
			out.flush();
			out.close();
			//res.sendRedirect("LeaveToList.do");
		}else if("/LeaveDoMultiDel.do".equals(path)){
			String[]qj_ids=req.getParameterValues("qj_ids");
			service.removeLeavesById(qj_ids);	
			res.sendRedirect("LeaveToList.do");
		}else if("/LeaveToUpd.do".equals(path)){
			String qj_id=req.getParameter("qj_id");
			Leave leave=service.getLeaveById(qj_id);
			req.setAttribute("Leave", leave);
			req.getRequestDispatcher("LeaveUpd.jsp").forward(req, res);
		}else if("/LeaveDoUpd.do".equals(path)){
			LeaveDto dto;
			try {
				dto=RequestBean.getBeanFromRequest(LeaveDto.class, req);
				service.modifyLeave(dto);
				res.sendRedirect("LeaveToList.do");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("/LeaveToSubmit.do".equals(path)){
			res.setContentType("application/json;charset=utf-8");
			PrintWriter out=res.getWriter();
			try {
				String qj_id=req.getParameter("qj_id");
				service.modifyLeaveStateToSubmited(qj_id);
				out.print("{\"success\":true,\"msg\":\"提交成功！！\"}");
			} catch (Exception e) {
				e.printStackTrace();
				out.print("{\"success\":false,\"msg\":\"提交失败！！\"}");
			}
			res.sendRedirect("LeaveToList.do");
		}else if("/LeaveToAgree.do".equals(path)){
			res.setContentType("application/json;charset=utf-8");
			PrintWriter out=res.getWriter();
			try {
				String qj_id=req.getParameter("qj_id");
				String sp_r=((Emp)session.getAttribute("Emp")).getE_name();
				service.modifyLeaveStateToAgree(qj_id,sp_r);
				out.print("{\"success\":true,\"msg\":\"请假申请已批准！！\"}");
			} catch (Exception e) {
				e.printStackTrace();
				out.print("{\"success\":false,\"msg\":\"请假申请没有批准成功！！\"}");
			}
			out.flush();
			out.close();
			//res.sendRedirect("LeaveToList.do");
		}else if("/LeaveToDisagree.do".equals(path)){
			res.setContentType("application/json;charset=utf-8");
			PrintWriter out=res.getWriter();
			try {
				String qj_id=req.getParameter("qj_id");
				String sp_r=((Emp)session.getAttribute("Emp")).getE_name();
				service.modifyLeaveStateToDisagree(qj_id, sp_r);
				out.print("{\"success\":true,\"msg\":\"请假申请已驳回！！\"}");
			} catch (Exception e) {
				e.printStackTrace();
				out.print("{\"success\":false,\"msg\":\"请假申请驳回失败！！\"}");
			}
			out.flush();
			out.close();
			//res.sendRedirect("LeaveToList.do");
		}else if("/LeaveToRecord.do".equals(path)){
			res.setContentType("application/json;charset=utf-8");
			PrintWriter out=res.getWriter();
			try {
				String qj_id=req.getParameter("qj_id");
				String ba_r=((Emp)session.getAttribute("Emp")).getE_name();
				//System.out.println("请假状态1："+req.getParameter("qj_state"));
				service.modifyLeaveToRecord(qj_id,ba_r);
				//System.out.println("+++++++++++==="+service.modifyLeaveToRecord(qj_id,ba_r));
				//System.out.println("请假状态2："+req.getParameter("qj_state"));
				out.print("{\"success\":true,\"msg\":\"备案成功！！\"}");
			} catch (Exception e) {
				e.printStackTrace();
				out.print("{\"success\":false,\"msg\":\"备案失败！！\"}");
			}
			out.flush();
			out.close();
			//res.sendRedirect("LeaveToList.do");
		}else if("/leaveInfo.do".equals(path)){
			String qj_id=req.getParameter("qj_id");
			req.setAttribute("leave",service.getLeaveById(qj_id));
			req.getRequestDispatcher("leaveInfo.jsp").forward(req, res);
		}else if("/LeaveToShenPi.do".equals(path)){
			LeaveDto dto = null;
			try {
				Emp emp=(Emp)session.getAttribute("Emp");
				dto = RequestBean.getBeanFromRequest(LeaveDto.class, req);
				System.out.println(dto.getQj_time());
				req.setAttribute("pg",service.QueryAllSubmitedLeaves(dto,emp));
				req.getRequestDispatcher("ShenPiList.jsp").forward(req, res);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}else if("/LeaveToBeiAn.do".equals(path)){
			LeaveDto dto = null;
			try {
				dto = RequestBean.getBeanFromRequest(LeaveDto.class, req);
				req.setAttribute("pg",service.QueryAllCheckedLeaves(dto));
				req.getRequestDispatcher("BeiAnList.jsp").forward(req, res);
			} catch (Exception e) {
				e.printStackTrace();
			} 	
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		this.doGet(req, res);
	}
}
